import uuid
from contextlib import asynccontextmanager
from fastapi import FastAPI
from loguru import logger
from utils.cache import TDDRedis
from utils.tdd_consul import TDDConsul
from datetime import datetime
tdd_consul = TDDConsul()

@asynccontextmanager
async def lifespan(app:FastAPI):
    logger.remove()
    logger.add("logs/file.log", rotation="500 MB", enqueue=True, level='INFO')
    tdd_consul.register()
    await tdd_consul.fetch_user_service_addresses()
    logger.info(f'用户服务已启动{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
    yield
    await TDDRedis().close()
    tdd_consul.deregister()
    logger.info(f'用户服务已关闭{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')

